const gameWidth = parseFloat(getComputedStyle(gameDom).width)

class Pipe extends dad{
    constructor(height,top,speed,dom){
        super( 52, height, gameWidth, top,speed,0,dom)
    }

    onMove () {
        if(this.left < -this.width){
            this.dom.remove()
        }
    }

}

function getRandom(min, max){
    return Math.floor(Math.random()*(max - min) + min)

}



class PipePare{
    constructor( speed){
        this.spaceHeight = 150;
        this.minHeight = 80;
        this.maxHeight = landtop - this.spaceHeight -this.minHeight;
        const upHeight = getRandom(this.minHeight,this.maxHeight);

        const upDom = document.createElement("div");
        upDom.className = "pipe up";


        this.upPipe = new Pipe(upHeight,0,speed,upDom);
        const downHeight = landtop - upHeight - this.spaceHeight;
        const downTop = landtop - downHeight;

        const downDom = document.createElement("div");
        downDom.className = "pipe down";

        this.downPipe = new Pipe(downHeight,downTop,speed,downDom);

        gameDom.appendChild(upDom);
        gameDom.appendChild(downDom);

    }

    get useLess(){//判断柱子是否没用
        return this.upPipe.left < -this.upPipe.width;
    }
    
    move(atime){
        this.upPipe.move(atime);
        this.downPipe.move(atime);
    }
}

class PipeCreat {
    constructor(speed){
        this.speed = speed;
        this.pairs = [];
        this.timer = null;
        this.tick  =1500;
    }

    starProduce(){
        this.timer = setInterval(() => {
            this.pairs.push(new PipePare(this.speed));
            for (let i = 0; i < this.pairs.length; i++) {
                const pair = this.pairs[i];
                if(pair.useLess){
                    this.pairs.splice(i,1);
                    i--;
                }
                
            }

        },this.tick );
    }

    stopProduce(){
        clearInterval(this.timer);
        this.timer= null;

    }

}



